What is d3-random?
The d3-random package is part of the D3 (Data-Driven Documents) JavaScript library, which is primarily used for generating random numbers with specific statistical properties. This package is useful for simulations, animations, and generating sample data for testing or visualization purposes.
What are d3-random's main functionalities?
Uniform Distribution
Generates a random number following a uniform distribution. In the provided code, random numbers between 1 and 5 are generated.
const d3 = require('d3-random');
const randomUniform = d3.randomUniform(1, 5);
console.log(randomUniform());
Normal Distribution
Generates a random number following a normal (Gaussian) distribution. The code example creates a standard normal distribution with a mean of 0 and a standard deviation of 1.
const d3 = require('d3-random');
const randomNormal = d3.randomNormal(0, 1);
console.log(randomNormal());
Log-Normal Distribution
Generates a random number following a log-normal distribution. The code example uses a mean of 0 and a standard deviation of 1 for the underlying normal distribution.
const d3 = require('d3-random');
const randomLogNormal = d3.randomLogNormal(0, 1);
console.log(randomLogNormal());
Bates Distribution
Generates a random number following a Bates distribution, which is useful for simulating the average of samples from a uniform distribution. The code example averages 10 samples.
const d3 = require('d3-random');
const randomBates = d3.randomBates(10);
console.log(randomBates());
Irwin-Hall Distribution
Generates a random number following an Irwin-Hall distribution. The code example sums 10 samples from a uniform distribution.
const d3 = require('d3-random');
const randomIrwinHall = d3.randomIrwinHall(10);
console.log(randomIrwinHall());
Other packages similar to d3-random
random-js
random-js is a mathematically correct random number generator library for JavaScript. It offers a variety of distributions and utilities similar to d3-random but with a broader focus on overall randomness utilities, including seeding capabilities which d3-random lacks.
chance
Chance is a minimalist generator of random strings, numbers, etc. to help reduce some monotony particularly while writing automated tests or anywhere else you need anything random. It provides more types of random generators compared to d3-random, such as random names, addresses, and more complex data types.
d3-random
Generate random numbers from various distributions.
Installing
If you use NPM, npm install d3-random
. Otherwise, download the latest release. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3
global is exported:
<script src="https://d3js.org/d3-random.v1.min.js"></script>
<script>
var random = d3.randomUniform(1, 10);
</script>
Try d3-random in your browser.
API Reference
# d3.randomUniform([min, ][max]) <>
Returns a function for generating random numbers with a uniform distribution. The minimum allowed value of a returned number is min, and the maximum is max. If min is not specified, it defaults to 0; if max is not specified, it defaults to 1. For example:
d3.randomUniform(6)();
d3.randomUniform(1, 5)();
Note that you can also use the built-in Math.random to generate uniform distributions directly. For example, to generate a random integer between 0 and 99 (inclusive), you can say Math.random() * 100 | 0
.
# d3.randomNormal([mu][, sigma]) <>
Returns a function for generating random numbers with a normal (Gaussian) distribution. The expected value of the generated numbers is mu, with the given standard deviation sigma. If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1.
# d3.randomLogNormal([mu][, sigma]) <>
Returns a function for generating random numbers with a log-normal distribution. The expected value of the random variable’s natural logarithm is mu, with the given standard deviation sigma. If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1.
# d3.randomBates(n) <>
Returns a function for generating random numbers with a Bates distribution with n independent variables.
# d3.randomIrwinHall(n) <>
Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables.
# d3.randomExponential(lambda) <>
Returns a function for generating random numbers with an exponential distribution with the rate lambda; equivalent to time between events in a Poisson process with a mean of 1 / lambda. For example, exponential(1/40) generates random times between events where, on average, one event occurs every 40 units of time.
# random.source(source)
Returns the same type of function for generating random numbers but where the given random number generator source is used as the source of randomness instead of Math.random. The given random number generator must implement the same interface as Math.random and only return values in the range [0, 1). This is useful when a seeded random number generator is preferable to Math.random. For example:
var d3 = require("d3-random"),
seedrandom = require("seedrandom"),
random = d3.randomNormal.source(seedrandom("a22ebc7c488a3a47"))(0, 1);
random();